package com.diandian.orc.tesseract;

import net.sourceforge.tess4j.Tesseract;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * Created by WSK on 2017/1/6.
 */
public class OCRTest {
    /**
     *
     * @param srImage 图片路径
     * @param ZH_CN 是否使用中文训练库,true-是
     * @return 识别结果
     */
    public static String FindOCR(String srImage, boolean ZH_CN) {
        try {
            System.out.println("start");
            double start=System.currentTimeMillis();
            File imageFile = new File(srImage);
            if (!imageFile.exists()) {
                return "图片不存在";
            }
            BufferedImage textImage = ImageIO.read(imageFile);
            Tesseract instance=new Tesseract();
            instance.setDatapath("C:\\Program Files (x86)\\Tesseract-OCR\\tessdata");//设置训练库
            //C:\Program Files (x86)\Tesseract-OCR
            if (ZH_CN)
                instance.setLanguage("chi_sim");//中文识别
            String result = null;
            result = instance.doOCR(textImage);
            double end=System.currentTimeMillis();
            System.out.println("耗时"+(end-start)/1000+" s");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return "发生未知错误";
        }
    }
    public static void main(String[] args) throws Exception {
        String result=FindOCR("C:\\Users\\Administrator\\Desktop\\test.png",true);
        System.out.println(result);
        String result2=FindOCR("C:\\Users\\Administrator\\Desktop\\test2.png",true);
        System.out.println(result2);
         result2=FindOCR("C:\\Users\\Administrator\\Desktop\\timg.jpg",true);
        System.out.println(result2);
        result2=FindOCR("C:\\Users\\Administrator\\Desktop\\test3.png",true);
        System.out.println(result2);
    }
}